var util = require('../../utils/util.js');
const md = require('../../utils/md5.js');
const app = getApp();
const post = require('../../utils/post.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    total:'0.00'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that =this;
    var mebid = options.mebid;
    var where='';
    var url=''
    var datas = {};
    var time = util.formatTime(new Date());
    time = time.split(' ')[0];
    var datedetail = time.split('-');
    if(options.type == "caigou"){
      wx.setNavigationBarTitle({
        title: '采购返利',
      });
      that.setData({
        qian:"总共￥",
        total: options.money
      })
      url ="/BO/TR_Detail/Query";
      // where = "TranType like '%" + app.server.assettype[3]+"%' and AccID =" + mebid + " and AccDate > '" + datedetail[0] + "-" + datedetail[1] + "-1" + " 00:00:00' and AccDate < '" + time + " 23:59:59'";
      where = "TranType like '%" + app.server.assettype[3] + "%' and AccID =" + mebid;
      
    }else{
      if(options.type == 'huibao'){
        wx.setNavigationBarTitle({
          title: '结余提成',
        });
        that.setData({
          qian: "昨日:",
          total: options.money +'%'
        })
        url = "/BO/TR_ProfitRate/Query";
        // where = "_BOState = 100 " + " and ProfitDate > '" + datedetail[0] + "-" + datedetail[1] + "-1" + " 00:00:00' and ProfitDate < '" + time + " 00:00:00'";
        where = "_BOState = 100  and ProfitDate < '" + time + " 00:00:00'";
        that.setData({
          hidesign: 1
        })
      }else{
        wx.setNavigationBarTitle({
          title: '累计收益',
        });
        that.setData({
          qian: "总共￥",
          total: options.money
        });
        url = "/BO/TR_Detail/Query";
        // where = "SrcType like '%" + app.server.assettype[2] +"%' and AccID =" + mebid + " and AccDate > '" + datedetail[0] + "-" + datedetail[1] + "-1" + " 00:00:00' and AccDate < '" + time + " 23:59:59'";
        where = "(TranType like '%" + app.server.assettype[2] + "%' or TranType like '%" + app.server.assettype[3]+"%') and AccID=" + mebid + " and AccDate <= '" + time + " 23:59:59'";
      }
      
    };
    // 获取返利详情
    // console.log(time);
    datas.where = where;
    datas.pageindex=1;
    datas.pagesize=100;
    datas.summary=1;
    console.log(where)
    var datacall = function (res) {
      console.log(res);
      if (res.data.data.length != 0) {
        var moneyinfo =res.data.data
        for(var i=0;i<res.data.data.length;i++){
          moneyinfo[i]['width'] = Math.round(moneyinfo[i]['MnyGet'] / res.data.summary.MnyGet * 10000) / 100.00 + "%";
          moneyinfo[i]['date'] = moneyinfo[i]['AccDate'].split('T')[0];
          moneyinfo[i]['date'] = moneyinfo[i]['AccDate'].split(' ')[0];
          moneyinfo[i]['date'] = moneyinfo[i]['date'].replace("-",".");
          moneyinfo[i]['date'] = moneyinfo[i]['date'].replace("-", ".");
          moneyinfo[i]['MnyGet'] = post.toDecimal2(moneyinfo[i]['MnyGet']);
        }
        that.setData({
          moneyinfo: moneyinfo,
          // total: post.toDecimal2(res.data.summary.MnyGet)
        })
      }
    }
    var datahuibao =function(res){
      console.log(res);
      if (res.data.data.length != 0) {
        var moneyinfo = res.data.data
        for (var i = 0; i < res.data.data.length; i++) {
          moneyinfo[i]['width'] = (moneyinfo[i]['ProfitRate']*1000) + "%";
          moneyinfo[i]['date'] = moneyinfo[i]['ProfitDate'].split('T')[0];
          moneyinfo[i]['date'] = moneyinfo[i]['ProfitDate'].split(' ')[0];
          moneyinfo[i]['date'] = moneyinfo[i]['date'].replace("-", ".");
          moneyinfo[i]['date'] = moneyinfo[i]['date'].replace("-", ".");
          moneyinfo[i]['MnyGet'] = moneyinfo[i]['ProfitRate'] + "%";
        }
        that.setData({
          moneyinfo: moneyinfo,
          // total: res.data.data[0].ProfitRate+'%'
        })
      }
    }
    var dataleiji =function(res){
      console.log(res);
      if (res.data.data.length != 0) {
        var moneyinfo = res.data.data;
        var map = {};
        var dest = [];
        //将相同日期归类于一起
        for (var i = 0; i < moneyinfo.length; i++) {
          var ai = moneyinfo[i];
          var indexs = ai.AccDate.split(' ')[0];
          if (!map[indexs]) {
            dest.push({
              TRID: indexs,
              name: ai.PayName,
              data: [ai]
            });
            map[indexs] = indexs;
          } else {
            for (var j = 0; j < dest.length; j++) {
              var dj = dest[j];
              if (dj.TRID == ai.AccDate.split(' ')[0]) {
                dest[j].data.push(ai);
                break;
              }
            }
          }
        }
        console.log(dest);
       //统计相同的日期金额
        for(var i=0;i<dest.length;i++){
          var sumt = 0;
          for(var j=0;j<dest[i].data.length;j++){
            sumt += dest[i].data[j].MnyHap;
          }
          dest[i].money = sumt;
          sumt =0;
        }
       //处理每个元素的所占百分比
        for(var i=0;i<dest.length;i++){
          dest[i]['width'] = Math.round(dest[i]['money'] / res.data.summary.MnyGet * 10000) / 100.00;
          if (dest[i]['width'] > 20){
            dest[i]['width']+="%";
          }else{
            dest[i]['width'] +=20;
            dest[i]['width'] += "%";
          }
          dest[i]['date'] = dest[i]['TRID'];
          dest[i]['MnyGet'] = post.toDecimal2(dest[i]['money']);
          dest[i]['_RowNo'] = i+1;
        }
        console.log(dest);
        // for (var i = 0; i < res.data.data.length; i++) {
        //   moneyinfo[i]['width'] = Math.round(moneyinfo[i]['MnyGet'] / res.data.summary.MnyGet * 10000) / 100.00;
        //   if (moneyinfo[i]['width'] > 20){
        //     moneyinfo[i]['width']+="%";
        //   }else{
        //     moneyinfo[i]['width'] +=20;
        //     moneyinfo[i]['width'] += "%";
        //   }
        //   moneyinfo[i]['date'] = moneyinfo[i]['AccDate'].split('T')[0];
        //   moneyinfo[i]['date'] = moneyinfo[i]['AccDate'].split(' ')[0];
        //   moneyinfo[i]['date'] = moneyinfo[i]['date'].replace("-", ".");
        //   moneyinfo[i]['date'] = moneyinfo[i]['date'].replace("-", ".");
        //   moneyinfo[i]['MnyGet'] = post.toDecimal2(moneyinfo[i]['MnyGet']);
        // }
        that.setData({
          // moneyinfo: moneyinfo,
          moneyinfo: dest,
          // total: post.toDecimal2(res.data.summary.MnyGet)
        })
      }
    }
    if (options.type == "caigou") {
      var res2 = post.getdata(url, datas, datacall);
    } else {
      if(options.type =="huibao"){
        var res2 = post.getdata(url, datas, datahuibao);
      }else{
        var res2 = post.getdata(url, datas, dataleiji);
      }
     
    };
  },
  onShow: function () {
  
  },

})